using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[ExecuteInEditMode]
public class UIScreen : MonoBehaviour
{
		public enum Style
		{
				KeepingRatio,
				BasedOnWidth,
				FillKeepingRatio,
				FillScreen,
				None,
		}
	
		public Style style = ScreenStyle;
		public Camera uiCamera = null;
		public double relativeSize = 0.004166667;
		public Vector2 screenSize = ScreenSize;
		public double relativeWH = 0.668;

		// Use this for initialization
		void Awake ()
		{
				if (uiCamera == null) {
						uiCamera = ComponentUtils.fetchParentComponent<Camera> (gameObject);
						if (uiCamera == null) {
								return;
						}
				}

				ScreenCamera = uiCamera;
				Transform screenTrans = uiCamera.transform;
				screenTrans = screenTrans.parent == null ? screenTrans : screenTrans.parent;
				Vector3 localScale = screenTrans.localScale;
				if (screenSize.y > 0) {
						localScale.x = localScale.y = localScale.z = (float)((double)480 / screenSize.y * relativeSize);
						screenTrans.localScale = localScale;
						if (screenSize.x > 0) {
								gameObject.transform.localScale = Vector3.one;
								if (!Application.isPlaying) {
										return;
								}
				
								float scaleX = uiCamera.pixelWidth / uiCamera.pixelHeight;
								if (scaleX > relativeWH) {
										scaleX = (float)relativeWH;
								}

								ScreenScaleX = scaleX;
								scaleX *= screenSize.y / screenSize.x;
								if (scaleX != 1.0f) {
										localScale = Vector3.one;
										if (VieworUtils.setAutoLayout (gameObject.transform, scaleX, screenSize.y)) {
												// Debug.Log("VieworUtils.setAutoLayout" + scaleX);
												localScale.x = localScale.y = localScale.z = scaleX;
												GameObjectUtils.getGameObjectComponent<UIFix> (gameObject);
												UIViewAuto viewAuto = GameObjectUtils.getGameObjectComponent<UIViewAuto> (gameObject);
												viewAuto.contentSize.x = screenSize.x;
												viewAuto.contentSize.y = screenSize.y;
												viewAuto.setAutoScaleX (scaleX);
						
										} else if (style == Style.FillScreen) {
												localScale.x = scaleX;
						
										} else {
												if (style == Style.FillKeepingRatio) {
														if (scaleX < 1.0f) {
																scaleX = 1.0f;
														}
							
												} else if (style == Style.KeepingRatio) {
														if (scaleX > 1.0f) {
																scaleX = 1.0f;
														}
							
												} else if (style == Style.None) {
														scaleX = 1.0f;
												}
						
												localScale.x = localScale.y = localScale.z = scaleX;
										}
					
										//gameObject.transform.localScale = localScale;
										Vector3 screenLocalScale = screenTrans.localScale;
										screenLocalScale.x *= localScale.x;
										screenLocalScale.y *= localScale.y;
										screenLocalScale.z *= localScale.z;
										screenTrans.localScale = screenLocalScale;
								}
						}
				}

				initScreenObject ();
		}

		private static Camera ScreenCamera = null;
		private static float ScreenScaleX = 1;
		private static Style ScreenStyle = Style.KeepingRatio;
		private static Vector2 ScreenSize = new Vector2 (640, 960);
		private static GameObject ScreenObject = null;
		private static GameObject UnactiveGameObject = null;
		private static GameObject DialogBackGroundObject = null;
		private static GameObject DialogBackGroundObjectCostumer = null;
		private static GameObject DialogBackGroundShow = null;
		private static Stack<GameObject> DialogObjectStack = null;
		private static float PixelSizeAdjustment;

		private void initScreenObject ()
		{
				//Debug.Log("initScreenObject");
				//UIViewAuto viewAuto = view
				ScreenStyle = style;
				ScreenSize = screenSize;
				ScreenObject = gameObject;
				if (UnactiveGameObject == null || !UnactiveGameObject) {
						UnactiveGameObject = new GameObject ("_UnactiveGameObject");
						UnactiveGameObject.SetActive (false);
						//UnactiveGameObject.AddComponent<UIPanel> ();
						UnactiveGameObject.transform.localPosition = new Vector3 (Screen.width * 100.0f, Screen.height * 100.0f, 0);
				}
        
				if (DialogBackGroundObject == null || !DialogBackGroundObject) {
						DialogBackGroundObject = Instantiate (Resources.Load ("DialogBackGroundObject")) as GameObject;
						setDialogBackGroundObject (DialogBackGroundObject);
						DialogBackGroundObjectCostumer = null;
				}

				closeDialogBackGroundObject ();

				/*
        if (DialogObjectStack != null)
        {
            GameObject dialogObject;
            while ((dialogObject = DialogObjectStack.Pop()) != null)
            {
                if (dialogObject)
                {
                    GameObject.Destroy(dialogObject);
                }
            }
        }*/

				DialogObjectStack = new Stack<GameObject> ();
		}

		public static Camera getScreenCamera ()
		{
				return ScreenCamera;
		}

		public static float getScreenScaleX ()
		{
				return ScreenScaleX;
		}
	
		public static Style getScreenStyle ()
		{
				return ScreenStyle;
		}

		public static Vector2 getScreenSize ()
		{
				return ScreenSize;
		}
	
		public static GameObject getScreenObject ()
		{
				return ScreenObject;
		}

		public static GameObject getUnactiveGameObject ()
		{
				return UnactiveGameObject;
		}

		public static void closeDialogBackGroundObject ()
		{
				if (DialogBackGroundShow != null && DialogBackGroundShow) {
						VieworUtils.removeView (DialogBackGroundShow.transform);
				}

				DialogBackGroundShow = null;
		}

		public static void showDialogBackGroundObject (GameObject dialogBackGroundObject)
		{
				closeDialogBackGroundObject ();
				DialogBackGroundShow = dialogBackGroundObject;
				VieworUtils.addView (DialogBackGroundShow.transform, ScreenObject.transform);
		}

		public static GameObject getDialogBackGroundObject (bool customer)
		{
				return DialogBackGroundObjectCostumer == null || !customer ? DialogBackGroundObject : DialogBackGroundObjectCostumer;
		}

		public static void setDialogBackGroundObject (GameObject dialogBackGroundObject)
		{
				if (DialogBackGroundObjectCostumer != null && DialogBackGroundObjectCostumer) {
						GameObject.Destroy (DialogBackGroundObjectCostumer);
				}

				DialogBackGroundObjectCostumer = dialogBackGroundObject;
				if (dialogBackGroundObject != null) {
						GameObjectUtils.getGameObjectComponent<UIRetain> (dialogBackGroundObject).retain ();
						GameObjectUtils.getGameObjectComponent<BoxCollider> (dialogBackGroundObject).isTrigger = true;
		
						Vector3 screenScale = ScreenObject.transform.localScale;
						Vector3 localScale = dialogBackGroundObject.transform.localScale;
						localScale.x = ScreenSize.x / screenScale.x * 2.0f;
						localScale.y = ScreenSize.y / screenScale.y * 2.0f;
						dialogBackGroundObject.transform.localScale = localScale;
		
						VieworUtils.addView (dialogBackGroundObject.transform, UnactiveGameObject.transform);
				}
		}

		public static Stack<GameObject> getDialogObjectStack ()
		{
				return DialogObjectStack;
		}
}
